package com.siyuan.blog.web.system;/*
 *@program:ejiajia

 *@description:用户注册登录

 *@author:X-C

 *@create:2020-05-29 02:08:28
 **/

import com.siyuan.blog.service.system.CusService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import com.siyuan.blog.domain.system.Cus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Controller
@RequestMapping("cus")
public class CusController {


    /**
     * 注册成功后返回登录页面
     *
     * @param cus
     * @return login.html
     */
    @Resource
    private CusService cusService;

    @PostMapping("regist")
    public String UserRegist(Cus cus) {
        int i = cusService.md5Password(cus);
        if (i >= 1) {
            return "uiceng/login";
        } else {
            return "uiceng/404";
        }
    }

    @PostMapping("login")
    public String UserLogin(Cus cus, Model model){
        UsernamePasswordToken token = new UsernamePasswordToken(cus.getCount(), cus.getPassword());
        SecurityUtils.getSubject().login(token);
        //获取登录成功的用户信息，同时保存在model
        model.addAttribute("CusInfo",SecurityUtils.getSubject().getPrincipal());
        return "uiceng/index";
    }
}
